package com.army.stock.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.army.stock.dao.ArmyMonthplanDao;
import com.army.stock.domain.ArmyMonthplan;
import com.army.stock.domain.ArmyMonthplanItem;
import com.army.stock.domain.ArmyYearplan;
import com.army.stock.service.ArmyMonthplanService;
import com.javalife.common.domain.page.PaginatedArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: zhangyong
 * Date: 2015/3/30
 * Time: 21:08
 * To change this template use File | Settings | File Templates.
 */
public class ArmyMonthplanServiceImpl implements ArmyMonthplanService {

    private static final Logger logger = LoggerFactory.getLogger(ArmyMonthplanServiceImpl.class);

    private ArmyMonthplanDao armyMonthplanDao;

    public void setArmyMonthplanDao(ArmyMonthplanDao armyMonthplanDao) {
        this.armyMonthplanDao = armyMonthplanDao;
    }

    @Override
    public PaginatedArrayList getArmyMonthplanPageInfo(int currentPage, int pageSize, Map<String, Object> map) {
        int startNum = (currentPage - 1) * pageSize;
        map.put("start", startNum);
        map.put("pageSize", pageSize);
        Integer count = armyMonthplanDao.getCountArmyMonthPlan(map);
        List<ArmyMonthplan> monthplans = armyMonthplanDao.getArmyMonthPlans(map);
        PaginatedArrayList<ArmyMonthplan> paginatedArrayList = new PaginatedArrayList<ArmyMonthplan>(currentPage, pageSize);
        paginatedArrayList.setTotalItem(count);
        paginatedArrayList.addAll(monthplans);
        return paginatedArrayList;
    }

    //重点的json
    //{"monthplanName":"2015年二炮 4月计划","departmentId":"1","items":[{"good_num":"1","goodPrice":"50.0","goodId":"3"},{"good_num":"1","goodPrice":"10000.0","goodId":"2"},{"good_num":"1","goodPrice":"5099.0","goodId":"4"},{"good_num":"1","goodPrice":"19.01","goodId":"5"}],"priceSum":"15168.01","year":"2015","month":"4"}
    public Long addArmyMonthplan(String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        String monthplanName = jsonObject.getString("monthplanName");
        String departmentId = jsonObject.getString("departmentId");
        String year = jsonObject.getString("year");
        String month = jsonObject.getString("month");
        String priceSum = jsonObject.getString("priceSum");
        JSONArray items = jsonObject.getJSONArray("items");

        //插入到月度计划表里
        ArmyMonthplan armyMonthplan = new ArmyMonthplan();
        armyMonthplan.setDepartmentId(Long.valueOf(departmentId));
        armyMonthplan.setMonth(month);
        armyMonthplan.setMonthplanName(monthplanName);
        armyMonthplan.setYear(year);
        armyMonthplan.setPriceSum(new BigDecimal(priceSum));
        armyMonthplan.setStatus(0);
        armyMonthplanDao.insertAmryMonthPlan(armyMonthplan);

        //插入到月度计划明细
        for (int i = 0; i < items.size(); i++) {
            JSONObject item = items.getJSONObject(i);
            ArmyMonthplanItem armyMonthplanItem = new ArmyMonthplanItem();
            armyMonthplanItem.setMonthPlanId(armyMonthplan.getId());
            armyMonthplanItem.setGoodId(item.getLongValue("goodId"));
            armyMonthplanItem.setGoodNum(Integer.valueOf(item.getIntValue("good_num")));
            armyMonthplanItem.setGoodPrice(item.getBigDecimal("goodPrice"));
            armyMonthplanItem.setSubtotal(
                    new BigDecimal(armyMonthplanItem.getGoodNum()).
                            multiply(armyMonthplanItem.getGoodPrice()));
            armyMonthplanDao.insertAmryMonthPlanItem(armyMonthplanItem);
        }
        return armyMonthplan.getId();
    }

    @Override
    public void addArmyMonthplan(ArmyMonthplan armyMonthplan) {

    }

    @Override
    public Map<String, Object> addArmyMonthplan(JSONObject jsonObject) {
        return null;
    }

    @Override
    public ArmyMonthplan getArmyMonthplanById(Long id) {
        return armyMonthplanDao.getArmyMonthPlanById(id);
    }

    @Override
    public PaginatedArrayList getArmyMonthplanItemsPageInfo(int currentPage, int pageSize, Map<String, Object> map) {
        int startNum = (currentPage - 1) * pageSize;
        map.put("start", startNum);
        map.put("pageSize", pageSize);
        Integer count = armyMonthplanDao.getCountArmyMonthPlanItems(map);
        List<Map> monthplanItems = armyMonthplanDao.getArmyMonthplanItems(map);
        PaginatedArrayList paginatedArrayList = new PaginatedArrayList(currentPage, pageSize);
        paginatedArrayList.setTotalItem(count);
        paginatedArrayList.addAll(monthplanItems);
        return paginatedArrayList;
    }

    @Override
    public List getAllArmyMonthplanItems(Long id) {
        return null;
    }

    @Override
    public Map<String, Object> updateArmyMonthplan(JSONObject jsonObject) {
        Map<String, Object> returnMap = new HashMap<>();
        Long monthPlanId = jsonObject.getLong("monthPlanId");
        Map param = new HashMap();
        param.put("monthPlanId", monthPlanId);
        Integer affectedRows = armyMonthplanDao.deleteAllArmyMonthPlanItems(monthPlanId);
        logger.info("monthPlanId:{} affectedRows:{}", monthPlanId, affectedRows);
        BigDecimal priceSum = jsonObject.getBigDecimal("priceSum");
        JSONArray items = jsonObject.getJSONArray("items");
        for (int i = 0; i < items.size(); i++) {
            JSONObject item = items.getJSONObject(i);
            Integer good_num = item.getInteger("good_num");
            BigDecimal goodPrice = item.getBigDecimal("goodPrice");
            Long goodId = item.getLong("goodId");
            param.put("goodId", goodId);
            Map<String, Object> itemInfo = armyMonthplanDao.getArmyMonthplanItemByGoodId(param);
            if (itemInfo == null) {
                logger.info("直接入库信息：" + item.toJSONString());
                //直接入库
                ArmyMonthplanItem armyMonthplanItem = new ArmyMonthplanItem();
                armyMonthplanItem.setMonthPlanId(monthPlanId);
                armyMonthplanItem.setGoodId(item.getLongValue("goodId"));
                armyMonthplanItem.setGoodNum(good_num);
                armyMonthplanItem.setGoodPrice(goodPrice);
                armyMonthplanItem.setSubtotal(
                        new BigDecimal(armyMonthplanItem.getGoodNum()).
                                multiply(armyMonthplanItem.getGoodPrice()));
                armyMonthplanDao.insertAmryMonthPlanItem(armyMonthplanItem);
            } else {
                logger.info("修改月度明细信息：" + item.toJSONString());
                Map updateItemMap = new HashMap();
                updateItemMap.put("monthPlanId", monthPlanId);
                updateItemMap.put("good_num", good_num);
                updateItemMap.put("goodPrice", goodPrice);
                updateItemMap.put("subtotal", new BigDecimal(good_num).multiply(goodPrice));
                armyMonthplanDao.updateArmyMonthPlanItem(updateItemMap);
            }
        }
        armyMonthplanDao.cleanArmyMonthPlanAmount(monthPlanId);
        //查询出总的金额
        ArmyMonthplan armyMonthplan = armyMonthplanDao.getArmyMonthPlanById(monthPlanId);
        returnMap.put("armyMonthplan", armyMonthplan);
        return returnMap;
    }

    @Override
    public void reviewArmyMonthplan(ArmyMonthplan armyMonthplan) {

    }

    @Override
    public void applyArmyMonthplan(Long MonthplanId, Integer status) {
        Map map = new HashMap();
        map.put("monthPlanId", MonthplanId);
        map.put("status", status);
        armyMonthplanDao.updateArmyMonthPlan(map);
    }

    @Override
    public PaginatedArrayList getArmyMonthplanPageInfoForFinance(int currentPage, int pageSize, Map<String, Object> map) {
        int startNum = (currentPage - 1) * pageSize;
        map.put("start", startNum);
        map.put("pageSize", pageSize);
        Integer count = armyMonthplanDao.getCountArmymonthplansFinance(map);
        List yearplans = armyMonthplanDao.getArmyMonthplansMapList(map);
        PaginatedArrayList<ArmyYearplan> paginatedArrayList = new PaginatedArrayList<ArmyYearplan>(currentPage, pageSize);
        paginatedArrayList.setTotalItem(count);
        paginatedArrayList.addAll(yearplans);
        return paginatedArrayList;
    }

    @Override
    public List<ArmyMonthplan> getArmyMonthplanByDepartmentId(Long departmentId) {
        return null;
    }

    public boolean checkExist(Long monthPlanId, Long goodid) {
        Map param = new HashMap();
        param.put("goodid", goodid);
        param.put("monthPlanId", monthPlanId);
        Integer count = armyMonthplanDao.getCountArmyMonthPlanItems(param);
        if (count == 0) {
            return false;
        }
        return true;
    }

    //月度统计
    public PaginatedArrayList getArmyMonthplanItemsPageInfoForInstorage(int currentPage, int pageSize, Map<String, Object> map) {
        int startNum = (currentPage - 1) * pageSize;
        map.put("start", startNum);
        map.put("pageSize", pageSize);
        Integer count = armyMonthplanDao.getCountArmyMonthItemForStorage(map);
        List monthItemsForStorage = armyMonthplanDao.getArmyMonthItemForStorage(map);
        PaginatedArrayList<ArmyYearplan> paginatedArrayList = new PaginatedArrayList<ArmyYearplan>(currentPage, pageSize);
        paginatedArrayList.setTotalItem(count);
        paginatedArrayList.addAll(monthItemsForStorage);
        return paginatedArrayList;
    }

    public List<Map> getAllArmyMonthplanItems(Map map) {
        return armyMonthplanDao.getAllArmyMonthplanItems(map);
    }

    public List<Map> getmonthplanssummary(Map map) {
        return armyMonthplanDao.getmonthplanssummary(map);
    }

    public BigDecimal getSummonthplanssummary(Map map) {
        return armyMonthplanDao.getSummonthplanssummary(map);
    }

}
